#include "Model.h"

Model::Model(QObject *parent)
    : QObject(parent)
{
}

QString Model::OpenLoadNpkFileDlg()
{
    QString filePath = getFilePathByFileDlg();
    if (filePath.isEmpty()) {
        qDebug() << Q_FUNC_INFO << "not select npk file path";
        return "";
    }

    LoadNpk(filePath);
    return filePath;
}

void Model::LoadNpk(const QString &path)
{
    m_coder.Load(path);
    Q_EMIT LoadNpkFinished();
}

void Model::OpenExportFileDlg()
{
    QString dirPath = getDirPathByFileDlg();
    if (dirPath.isEmpty()) {
        qDebug() << Q_FUNC_INFO << "not select exporting dir path";
        return;
    }

    Export(dirPath);
}

void Model::Export(const QString &outputDirPath)
{
    const QList<Album> &albumList = m_coder.GetAlbumList();
    for (const Album &album : albumList) {
        const QString &albumImgOutputDirPath = QString("%1/Img/%2")
                                                .arg(outputDirPath)
                                                .arg(album.Path);
        QDir dir(albumImgOutputDirPath);
        if (!dir.exists()) {
            dir.mkpath(dir.absolutePath());
        }

        const QString &albumOffsetInfoLuaOutputDirPath = QString("%1/OffsetInfo/%2/Lua")
                                                   .arg(outputDirPath)
                                                   .arg(album.Path);
        dir.setPath(albumOffsetInfoLuaOutputDirPath);
        if (!dir.exists()) {
            dir.mkpath(dir.absolutePath());
        }
        const QString &albumOffsetInfoJsonOutputDirPath = QString("%1/OffsetInfo/%2/Json")
                                                   .arg(outputDirPath)
                                                   .arg(album.Path);
        dir.setPath(albumOffsetInfoJsonOutputDirPath);
        if (!dir.exists()) {
            dir.mkpath(dir.absolutePath());
        }


        for (const SpriteStruct &sprite : album.SpriteList) {
            // output img
            const QString &imgFileOutputPath = QString("%1/%2.png")
                                                   .arg(albumImgOutputDirPath)
                                                   .arg(sprite.Index);
            const QImage &img = GetSpriteImg(album, sprite);
            img.save(imgFileOutputPath, "png");

            // output offset info lua
            int ox = 232 - sprite.X;
            int oy = 333 - sprite.Y;
            const QString &offsetInfoLuaFileOutputPath = QString("%1/%2.cfg")
                                                   .arg(albumOffsetInfoLuaOutputDirPath)
                                                   .arg(sprite.Index);
            QFile f(offsetInfoLuaFileOutputPath);
            if (!f.open(QIODevice::OpenModeFlag::WriteOnly)) {
                qCritical() << Q_FUNC_INFO << offsetInfoLuaFileOutputPath << "open failed!";
                continue;
            }

            const QString &luaContentStr = QString("return {\n    ox = %1,\n    oy = %2\n}")
                                            .arg(ox).arg(oy);
            f.write(luaContentStr.toUtf8());
            f.close();

            // output offset info json
            const QString &offsetInfoJsonFileOutputPath = QString("%1/%2.json")
                                                   .arg(albumOffsetInfoJsonOutputDirPath)
                                                   .arg(sprite.Index);
            f.setFileName(offsetInfoJsonFileOutputPath);
            if (!f.open(QIODevice::OpenModeFlag::WriteOnly)) {
                qCritical() << Q_FUNC_INFO << offsetInfoJsonFileOutputPath << "open failed!";
                continue;
            }

            const QString &josnContentStr = QString("{\n    \"ox\": \"%1\",\n    \"oy\": \"%2\"\n}")
                                            .arg(ox).arg(oy);
            f.write(josnContentStr.toUtf8());
            f.close();
        }
    }
}

const QList<Album> &Model::GetAlbumList()
{
    return m_coder.GetAlbumList();
}

QImage Model::GetSpriteImg(const Album &album, const SpriteStruct &sprite)
{
    return m_coder.GetSpriteImg(album, sprite);
}

QString Model::getFilePathByFileDlg()
{
    const QString &localDataDirPath = QStandardPaths::writableLocation(QStandardPaths::StandardLocation::DesktopLocation);

    QString defaultFilePath = QString("%1/").arg(localDataDirPath);

    QFileInfo fInfo(defaultFilePath);
    QDir dir = fInfo.dir();
    if (!dir.exists()) {
       dir.mkpath(dir.absolutePath());
    }

    QString filePath = QFileDialog::getOpenFileName(nullptr, "打开Npk", defaultFilePath, "*.npk");
    return filePath;
}

QString Model::getDirPathByFileDlg()
{
    const QString &localDataDirPath = QStandardPaths::writableLocation(QStandardPaths::StandardLocation::DesktopLocation);

    QString defaultDirPath = QString("%1/NpkOutput/").arg(localDataDirPath);

    QFileInfo fInfo(defaultDirPath);
    QDir dir = fInfo.dir();
    if (!dir.exists()) {
        dir.mkpath(dir.absolutePath());
    }

    QString dirPath = QFileDialog::getExistingDirectory(nullptr, "导出", defaultDirPath);
    return dirPath;
}
